package com.example.travelservice.controller;

import com.example.travelservice.pojo.Admin;
import com.example.travelservice.pojo.AdminLoginParam;
import com.example.travelservice.pojo.RespBean;
import com.example.travelservice.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * Created with IDEA
 *
 * @Auther 何祺颖 heqizen
 * @wechat hqzahqz
 * @date: 2022/01/15/21:57
 * @description:  登录
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {


    @Resource
    private IAdminService adminService;

    @ApiModelProperty(value = "登录后返回token")
    @PostMapping("/login")
    public RespBean login(AdminLoginParam adminLoginParam, HttpServletRequest request) {
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),request);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("admin/info")
    public Admin getAdminInfo(Principal principal) {
        if(principal == null){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        return admin;

    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出成功");
    }

}
